package com.techsoul.module.pms.controller;

import com.techsoul.common.core.pojo.Result;
import com.techsoul.common.core.pojo.vo.OrgVo;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.lang.StringUtils;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.pms.pojo.param.YunheFireOrEventMesgSaveParam;
import com.techsoul.module.pms.pojo.param.YunheFireOrEventMesgSearchParam;
import com.techsoul.module.pms.service.IYunheFireOrEventMesgService;
import com.techsoul.module.system.service.IOrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;

/**
 * mesg Controller
 *
 * @author liuen
 * @date 2021-09-23
 */
@Api(tags = "mesg")
@RestController
@RequestMapping("/mesg/yunheFireOrEventMesg")
public class YunheFireOrEventMesgController {

    @Autowired
    private Result result;
    
    @Autowired
    private IYunheFireOrEventMesgService service;

    @Autowired
    private IOrgService orgService;
    @Autowired
    private TokenService tokenService;
    @ApiOperation("新增")
    @PreAuthorize("hasAnyAuthority('mesg:yunheFireOrEventMesg:insert')")
    @PostMapping
    public Result insert(@Valid @RequestBody YunheFireOrEventMesgSaveParam params) {
        return result.success(this.service.insert(params));
    }
    @ApiOperation("预警消息")
    @PostMapping("/insertAlert")
    public Result insertAlert(@Valid @RequestBody YunheFireOrEventMesgSaveParam params) {
        if(ServletUtils.getRequest()!=null){
            if(tokenService.getLoginUser(ServletUtils.getRequest())!=null){
               if(tokenService.getLoginUser(ServletUtils.getRequest()).getUser()!=null){
                   UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
                   params.setNextProcessDepartment(sysUser.getOrgId());
                   OrgVo sysOrg = orgService.findById(sysUser.getOrgId());
                   params.setCreateBy(sysUser.getId());
                   params.setNextProcessDept(sysOrg.getOrgName());
                   return result.success(this.service.insertAlert(params));
               }
            }
        }
        params.setNextProcessDepartment("1351808618424569858");
        params.setCreateBy("1");
        params.setNextProcessDept("林业巡视部");
        return result.success(this.service.insertAlert(params));
    }
    @ApiOperation("鉴定")
    @PostMapping("/checkStatus")
    public Result checkStatus(@Valid @RequestBody YunheFireOrEventMesgSaveParam params) {
        if(ServletUtils.getRequest()!=null){
            if(tokenService.getLoginUser(ServletUtils.getRequest())!=null){
                if(tokenService.getLoginUser(ServletUtils.getRequest()).getUser()!=null){
                    UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
                    params.setNextProcessDepartment(sysUser.getOrgId());
                    OrgVo sysOrg = orgService.findById(sysUser.getOrgId());
                    params.setUpdateBy(sysUser.getId());
                    params.setCheckPerson(sysUser.getUserName());
                    params.setNextProcessDept(sysOrg.getOrgName());
                    params.setStatus("checked");
                    return result.success(this.service.update(params));
                }
            }
        }
        params.setNextProcessDepartment("1336595344791834624");
        params.setUpdateBy("1");
        params.setCheckPerson("柳项平");
        params.setNextProcessDept("林业巡视部");
        params.setStatus("checked");
        return result.success(this.service.update(params));
    }
    @ApiOperation("审核")
    @PostMapping("/proveStatus")
    public Result proveStatus(@Valid @RequestBody YunheFireOrEventMesgSaveParam params) {
        if(ServletUtils.getRequest()!=null){
            if(tokenService.getLoginUser(ServletUtils.getRequest())!=null){
                if(tokenService.getLoginUser(ServletUtils.getRequest()).getUser()!=null){
                    UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
                    params.setNextProcessDepartment(sysUser.getOrgId());
                    OrgVo sysOrg = orgService.findById(sysUser.getOrgId());
                    params.setUpdateBy(sysUser.getId());
                    params.setProvePerson(sysUser.getUserName());
                    params.setProveNote(params.getCheckNote());
                    params.setNextProcessDept(sysOrg.getOrgName());
                    params.setStatus("proved");
                    return result.success(this.service.update(params));
                }
            }
        }
        params.setNextProcessDepartment("1336595344791834624");
        params.setUpdateBy("1");
        params.setProvePerson("柳项平");
        params.setProveNote(params.getCheckNote());
        params.setNextProcessDept("林业巡视部");
        params.setStatus("proved");
        return result.success(this.service.update(params));
    }
    @ApiOperation("批量删除（多个用逗号隔开）")
   // @PreAuthorize("hasAnyAuthority('mesg:yunheFireOrEventMesg:delete')")
    @DeleteMapping("/{ids}")
    public Result batchDelete(@PathVariable String[] ids) {
        return result.success(this.service.batchDelete(Arrays.asList(ids)));
    }

    @ApiOperation("修改")
    //@PreAuthorize("hasAnyAuthority('mesg:yunheFireOrEventMesg:update')")
    @PutMapping
    public Result update(@Valid @RequestBody YunheFireOrEventMesgSaveParam params) {
        if(!StringUtils.isNull(params.getNextProcessDepartment())){
            OrgVo sysOrg = orgService.findById(params.getNextProcessDepartment());
            params.setNextProcessDept(sysOrg.getOrgName());
        }

        return result.success(this.service.update(params));
    }

    @ApiOperation("详情")
   // @PreAuthorize("hasAnyAuthority('mesg:yunheFireOrEventMesg:search')")
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {
        return result.success(this.service.findById(id));
    }

    @ApiOperation("我的列表")
    //@PreAuthorize("hasAnyAuthority('mesg:yunheFireOrEventMesg:search')")
    @PostMapping("/myCreateMesg")
    public Result myCreateMesg(@RequestBody YunheFireOrEventMesgSearchParam params) {
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        params.setCreateBy(sysUser.getId());

        return result.success(this.service.findAll(params));
    }
    @ApiOperation("我的待处理列表")
   // @PreAuthorize("hasAnyAuthority('mesg:yunheFireOrEventMesg:search')")
    @PostMapping("/myWaitProcessMesg")
    public Result myWaitProcessMesg(@RequestBody YunheFireOrEventMesgSearchParam params) {
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        params.setNextProcessDepartment(sysUser.getOrgId());

        return result.success(this.service.findAll(params));
    }
    @ApiOperation("列表")
    //@PreAuthorize("hasAnyAuthority('mesg:yunheFireOrEventMesg:search')")
    @PostMapping("/search")
    public Result findAll(@RequestBody YunheFireOrEventMesgSearchParam params) {
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        params.setNextProcessDepartment(sysUser.getOrgId());

        return result.success(this.service.findAll(params));
    }

    @ApiOperation("列表-分页")
   // @PreAuthorize("hasAnyAuthority('mesg:yunheFireOrEventMesg:search')")
    @PostMapping("/search/{current}/{pageSize}")
    public Result findAll(@RequestBody YunheFireOrEventMesgSearchParam params, @PathVariable Integer current, @PathVariable Integer pageSize) {
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        params.setNextProcessDepartment(sysUser.getOrgId());

        return result.success(this.service.findAll(params, current, pageSize));
    }

}
